package hhm.wechat.controller;

import java.io.IOException;
import java.util.Date;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.DocumentException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import hhm.wechat.constants.Wechat;
import hhm.wechat.pojo.TextMessager;
import hhm.wechat.utils.DecriptUtil;
import hhm.wechat.utils.MessageUtil;
import hhm.wechat.utils.StringUtil;

@Controller
@RequestMapping("/verify")
public class VerifyController {

	private Log logger = LogFactory.getLog(this.getClass());

	/**
	 * get请求验证微信url
	 * 
	 * @param request
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/doit", method = RequestMethod.GET)
	public void doGET(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 微信加密签名

		String signature = request.getParameter("signature");

		// 时间戳

		String timestamp = request.getParameter("timestamp");

		// 随机数

		String nonce = request.getParameter("nonce");

		// 随机字符串

		String echostr = request.getParameter("echostr");

		// 排序
		String sortString = StringUtil.sort(Wechat.TOKEN, timestamp, nonce);
		// 加密
		String mytoken = DecriptUtil.SHA1(sortString);
		// 校验签名

		logger.info("来了一次验证");
		if (mytoken != null && mytoken != "" && mytoken.equals(signature)) {
			response.getWriter().print(echostr);

		} else {

			logger.error("签名校验失败");

		}
	}

	@ResponseBody
	@RequestMapping(value = "/doit", method = RequestMethod.POST)
	public Object doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String result = null;

		try {

			Map<String, String> map = MessageUtil.xmlToMap(request);

			String msgType = map.get("MsgType");
			String toUserName = map.get("ToUserName");
			String fromUserName = map.get("FromUserName");

			logger.info("当前用户发送过来的行为是:" + msgType);

			if (msgType.equals("text")) {
				TextMessager textMessager = new TextMessager();
				textMessager.setFromUserName(toUserName);
				textMessager.setToUserName(fromUserName);
				textMessager.setMsgType("text");
				textMessager.setCreateTime(new Date());

				textMessager.setContent("wcnmd");

				result = MessageUtil.objectToXml(textMessager, TextMessager.class);
			} else if (msgType.equals("event")) {

				result = MessageUtil.createNewsMessage(toUserName, fromUserName);

			}

			logger.info("发送回去的数据是:" + result);

			return result;

		} catch (DocumentException e) {
			e.printStackTrace();
		}
		return response;

	}

	@ResponseBody
	@RequestMapping(value = "/test", method = RequestMethod.GET)
	public Object test(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// response.getWriter().write("中文乱码?");

		return "中文乱码?";

	}

}
